import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
 roles: ['admin','editor']    control the page roles (you can set multiple roles)
 title: 'title'               the name show in sidebar and breadcrumb (recommend set)
 icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
 breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
 activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
 }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: 'Dashboard',
      component: () => import('@/views/dashboard/index'),
      meta: { title: 'ERP管理系统', icon: 'dashboard' }
    }]
  },


  {
    path: '/client',
    component: Layout,
    redirect: '/client/manage',
    children: [{
      path: 'client',
      name: 'Client',
      component: () => import('@/views/client/index'),
      meta: { title: '客户管理', icon: 'el-icon-s-help' }
    }]
  },

  {
    path: '/supplier',
    component: Layout,
    redirect: '/supplier/manage',
    children: [{
      path: 'supplier',
      name: 'Supplier',
      component: () => import('@/views/supplier/index'),
      meta: { title: '供应商管理', icon: 'el-icon-s-help' }
    }]
  },

  {
    path: '/offer',
    component: Layout,
    redirect: '/offer/manage',
    children: [{
      path: 'offer',
      name: 'Offer',
      component: () => import('@/views/offer/index'),
      meta: { title: '报价单管理', icon: 'el-icon-s-help' }
    }]
  },

  {
    path: '/salesTicket',
    component: Layout,
    redirect: '/salesTicket/manage',
    children: [{
      path: 'salesTicket',
      name: 'SalesTicket',
      component: () => import('@/views/salesTicket/index'),
      meta: { title: '销售单管理', icon: 'el-icon-s-help' }
    }]
  },


  {
    path: '/purchase',
    component: Layout,
    redirect: '/purchase/manage',
    children: [{
      path: 'purchase',
      name: 'Purchase',
      component: () => import('@/views/purchase/index'),
      meta: { title: '采购管理', icon: 'el-icon-s-help' }
    }]
  },


  {
    path: '/inventoryManage',
    component: Layout,
    redirect: '/inventory/manage',
    name: 'inventory',
    meta: { title: '库存管理', icon: 'el-icon-s-help' },
    children: [
      {
        path: 'stockControl',
        name: 'stockControl',
        component: () => import('@/views/stockControl/index'),
        meta: { title: '库存管理', icon: 'table' }
      },
      {
        path: 'stockRemoval',
        name: 'stockRemoval',
        component: () => import('@/views/stockRemoval/index'),
        meta: { title: '出库管理', icon: 'tree' }
      },
      {
        path: 'intoTheRemoval',
        name: 'intoTheRemoval',
        component: () => import('@/views/intoTheRemoval/index'),
        meta: { title: '入库管理', icon: 'tree' }
      },
    ]
  },
  {
    path: '/mineQuest',
    component: Layout,
    redirect: '/mineQuest/manage',
    name: 'mineQuest',
    meta: { title: '我的任务', icon: 'el-icon-s-help' },
    children: [
      {
        path: 'mine',
        name: 'mine',
        component: () => import('@/views/mine/index'),
        meta: { title: '我的任务', icon: 'table' }
      },
      {
        path: 'nonProcess',
        name: 'nonProcess',
        component: () => import('@/views/nonProcess/index'),
        meta: { title: '非标加工任务', icon: 'tree' }
      },
      {
        path: 'examine',
        name: 'examine',
        component: () => import('@/views/examine/index'),
        meta: { title: '待审批', icon: 'tree' }
      },
    ]
  },
  {
    path: '/finance',
    component: Layout,
    redirect: '/finance/manage',
    name: 'finance',
    meta: { title: '财务管理', icon: 'el-icon-s-help' },
    children: [
      {
        path: 'expenseThe',
        name: 'expenseThe',
        component: () => import('@/views/expenseThe/index'),
        meta: { title: '费用支出', icon: 'table' }
      },
      {
        path: 'fundJoin',
        name: 'fundJoin',
        component: () => import('@/views/fundJoin/index'),
        meta: { title: '资金收入', icon: 'tree' }
      },
      {
        path: 'accountTurn',
        name: 'accountTurn',
        component: () => import('@/views/accountTurn/index'),
        meta: { title: '账户互转单', icon: 'tree' }
      },
    ]
  },
  {
    path: '/statement',
    component: Layout,
    redirect: '/statement/manage',
    name: 'statement',
    meta: { title: '报表', icon: 'el-icon-s-help' },
    children: [
      {
        path: 'sellRunningWater',
        name: 'sellRunningWater',
        component: () => import('@/views/sellRunningWater/index'),
        meta: { title: '销售流水', icon: 'table' }
      },
      {
        path: 'purchaseRunningWater',
        name: 'purchaseRunningWater',
        component: () => import('@/views/purchaseRunningWater/index'),
        meta: { title: '采购流水', icon: 'tree' }
      },
      {
        path: 'sellPerformance',
        name: 'sellPerformance',
        component: () => import('@/views/sellPerformance/index'),
        meta: { title: '销售业绩表', icon: 'tree' }
      },
      {
        path: 'receiveGoods',
        name: 'receiveGoods',
        component: () => import('@/views/receiveGoods/index'),
        meta: { title: '收货提醒表', icon: 'tree' }
      },
      {
        path: 'deliverGoods',
        name: 'deliverGoods',
        component: () => import('@/views/deliverGoods/index'),
        meta: { title: '送货提醒表', icon: 'tree' }
      },
    ]
  },
  {
    path: '/systemSetting',
    component: Layout,
    redirect: '/system/setting',
    name: 'system',
    meta: { title: '系统设置', icon: 'el-icon-s-help' },
    children: [
      {
        path: 'userRemoval',
        name: 'userRemoval',
        component: () => import('@/views/userRemoval/index'),
        meta: { title: '用户管理', icon: 'user' }
      },
      {
        path: 'roleRemoval',
        name: 'roleRemoval',
        component: () => import('@/views/roleRemoval/index'),
        meta: { title: '角色管理', icon: 'tree' }
      },
      {
        path: 'sectionRemoval',
        name: 'sectionRemoval',
        component: () => import('@/views/sectionRemoval/index'),
        meta: { title: '部门管理', icon: 'tree' }
      },
      {
        path: 'operationLog',
        name: 'operationLog',
        component: () => import('@/views/operationLog/index'),
        meta: { title: '操作日志', icon: 'tree' }
      },
    ]
  },


  // ----------------------------- 一下为原项目--------------------------------------
  {
    path: '/form',
    component: Layout,
    hidden: true,
    children: [
      {
        path: 'index',
        name: 'Form',
        component: () => import('@/views/form/index'),
        meta: { title: 'Form', icon: 'form' }
      }
    ]
  },

  {
    path: '/nested',
    component: Layout,
    hidden: true,
    redirect: '/nested/menu1',
    name: 'Nested',
    meta: {
      title: 'Nested',
      icon: 'nested'
    },
    children: [
      {
        path: 'menu1',
        component: () => import('@/views/nested/menu1/index'), // Parent router-view
        name: 'Menu1',
        meta: { title: 'Menu1' },
        children: [
          {
            path: 'menu1-1',
            component: () => import('@/views/nested/menu1/menu1-1'),
            name: 'Menu1-1',
            meta: { title: 'Menu1-1' }
          },
          {
            path: 'menu1-2',
            component: () => import('@/views/nested/menu1/menu1-2'),
            name: 'Menu1-2',
            meta: { title: 'Menu1-2' },
            children: [
              {
                path: 'menu1-2-1',
                component: () => import('@/views/nested/menu1/menu1-2/menu1-2-1'),
                name: 'Menu1-2-1',
                meta: { title: 'Menu1-2-1' }
              },
              {
                path: 'menu1-2-2',
                component: () => import('@/views/nested/menu1/menu1-2/menu1-2-2'),
                name: 'Menu1-2-2',
                meta: { title: 'Menu1-2-2' }
              }
            ]
          },
          {
            path: 'menu1-3',
            component: () => import('@/views/nested/menu1/menu1-3'),
            name: 'Menu1-3',
            meta: { title: 'Menu1-3' }
          }
        ]
      },
      {
        path: 'menu2',
        component: () => import('@/views/nested/menu2/index'),
        name: 'Menu2',
        meta: { title: 'menu2' }
      }
    ]
  },

  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
